<script>
	import Vue from 'vue';
	// 移除JS中的SCSS引用，改为在style中引用
	const CryptoJS = require("crypto-js");

	export default {
		onLaunch: function() {
			this.initLoginState();
			// 字符串加密方法挂载
			Vue.prototype.$strEncode = function($data) {
				if (typeof $data === 'object') {
					$data = JSON.stringify($data);
				}
				const key = CryptoJS.enc.Latin1.parse('BC1F5E3BAEE198AC');
				const iv = CryptoJS.enc.Latin1.parse('1F5E3BAEE198ACE1');
				const encoded = CryptoJS.AES.encrypt($data, key, {
					iv: iv,
					mode: CryptoJS.mode.CBC,
					padding: CryptoJS.pad.ZeroPadding
				}).toString();
				return encoded || '';
			};

			Vue.prototype.clientType = function() {
				let c = 1; // 默认为电脑
				// #ifdef APP-PLUS
				switch (uni.getSystemInfoSync().platform) {
					case 'android':
						c = 3;
						break;
					case 'ios':
						c = 4;
						break;
				}
				// #endif

				// #ifdef H5
				c = 2;
				// #endif

				// #ifdef MP-WEIXIN
				c = 5;
				// #endif
				return c;
			};

			const systemInfo = uni.getSystemInfoSync();
			// #ifndef MP
			Vue.prototype.StatusBar = systemInfo.statusBarHeight;
			Vue.prototype.CustomBar = systemInfo.platform === 'android' ?
				systemInfo.statusBarHeight + 50 :
				systemInfo.statusBarHeight + 45;
			// #endif

			// #ifdef MP-WEIXIN
			Vue.prototype.StatusBar = systemInfo.statusBarHeight;
			const custom = wx.getMenuButtonBoundingClientRect();
			Vue.prototype.Custom = custom;
			Vue.prototype.CustomBar = custom.bottom + custom.top - systemInfo.statusBarHeight;
			// #endif

			// #ifdef MP-ALIPAY
			Vue.prototype.StatusBar = systemInfo.statusBarHeight;
			Vue.prototype.CustomBar = systemInfo.statusBarHeight + systemInfo.titleBarHeight;
			// #endif

			// 关闭页面
			Vue.prototype.closePage = function() {
				console.log(getCurrentPages());
				if (getCurrentPages().length > 1) {
					uni.navigateBack();
				} else {
					// #ifdef H5
					history.back();
					// #endif
					// #ifndef H5
					uni.reLaunch({
						url: '/pages/index/index'
					});
					// #endif
				}
			};

			// 统一跳转接口
			Vue.prototype.navTo = function(url) {
				const whiteList = [
					'/pages/login/login',
					'/pages/login/forget',
					'/pages/login/reg',
					'/pages/base/about',
					'/pages/user/scan-login/index'
				];

				if (whiteList.includes(url)) {
					uni.navigateTo({
						url
					});
					return;
				}

				// 检查登录状态
				if (!this.$store || !this.$store.getters['user/isLogin']) {
					uni.showToast({
						title: '请先登录',
						icon: 'none'
					});
					uni.navigateTo({
						url: `/pages/login/login?redirect=${encodeURIComponent(url)}`
					});
					return;
				}

				uni.navigateTo({
					url
				});
			};
		},

		onShow: function() {
			this.initLoginState();
		},

		methods: {
			// 初始化登录状态
			initLoginState() {
				if (!this.$store) return;

				try {
					const token = uni.getStorageSync('token');
					const userData = uni.getStorageSync('userData');

					if (token && userData) {
						this.$store.commit('user/SET_TOKEN', {
							token,
							userData
						});
					}
				} catch (e) {
					console.error('初始化登录状态失败:', e);
				}
			}
		},

		onHide: function() {
			console.log('App Hide');
		}
	};
</script>

<style lang="scss">
	// 在style标签内引用SCSS文件，符合App平台v3模式要求
	@import "@/static/styles/common.scss";
</style>